#!/usr/bin/php -d mysqlnd.net_read_timeout=3600
<?
set_include_path("../../../include");
require("header.inc.php");

$startShard = !empty($argv[1]) ? $argv[1] : 1;

$shardIDs = Zotero_DB::columnQuery("SELECT shardID FROM shards WHERE shardID >= ? ORDER BY shardID", $startShard);
foreach ($shardIDs as $shardID) {
	echo "Shard: $shardID\n";
	
	// Requires Barracuda file format for indexes >767 bytes
	Zotero_Admin_DB::query("ALTER TABLE tags ROW_FORMAT=DYNAMIC", false, $shardID);
	Zotero_Admin_DB::query("ALTER TABLE syncDeleteLogKeys ROW_FORMAT=DYNAMIC", false, $shardID);
	Zotero_Admin_DB::query("ALTER TABLE tags CHANGE `name` `name` VARCHAR( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL", false, $shardID);
	Zotero_Admin_DB::query("ALTER TABLE syncDeleteLogKeys CHANGE `key` `key` VARCHAR( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL", false, $shardID);
	
	Zotero_Admin_DB::query("ALTER TABLE `itemSortFields` CHANGE `sortTitle` `sortTitle` VARCHAR( 79 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL, CHANGE `creatorSummary` `creatorSummary` VARCHAR( 50 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL", false, $shardID);
	
	Zotero_Admin_DB::query("ALTER TABLE `collections` CHANGE `collectionName` `collectionName` VARCHAR( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL", false, $shardID);
	Zotero_Admin_DB::query("ALTER TABLE `creators` CHANGE `firstName` `firstName` VARCHAR( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL, CHANGE `lastName` `lastName` VARCHAR( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL", false, $shardID);
	Zotero_Admin_DB::query("ALTER TABLE `itemData` CHANGE `value` `value` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL", false, $shardID);
	Zotero_Admin_DB::query("ALTER TABLE `itemNotes` CHANGE `note` `note` MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL, CHANGE `noteSanitized` `noteSanitized` MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NULL DEFAULT NULL, CHANGE `title` `title` VARCHAR( 80 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL", false, $shardID);
	Zotero_Admin_DB::query("ALTER TABLE `savedSearchConditions` CHANGE `value` `value` VARCHAR( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL", false, $shardID);
	Zotero_Admin_DB::query("ALTER TABLE `settings` CHANGE `value` `value` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL", false, $shardID);
}
